<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/29
 * Time: 10:13
 */

namespace App\HttpController\Admin;
use App\Services\HelpService;
use EasySwoole\Http\Response;
use EasySwoole\Http\Message\Status;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Jwt\Jwt;

class AdminBaseController extends Controller
{
    public $adminInfo = array();
    public $param = array();
    protected function onRequest(?string $action): ?bool
    {
        $token =  $this->request()->getHeaderLine('token');
        $content = $this->request()->getRequestParam('content');
        $timestamp = $this->request()->getRequestParam('timestamp');
        $nonce = $this->request()->getRequestParam('nonce');
        $sign = $this->request()->getRequestParam('sign');
        $nonce = HelpService::aesDecrypt($nonce);

        if(!$this->checkSign($content,$timestamp, $nonce,$sign)){
            // $this->writeJson(Status::CODE_UNAUTHORIZED,  array(),'呃呃呃呃呃，签名错误');
            //   return false;
        }

        $this->param =  json_decode(preg_replace('/[\x00-\x1F]/','', HelpService::aesDecrypt($content)),true);
        $param['_rand_num'] = time();#设置个默认表单，防止验证器报空错
        $whiteArr = array('Login','Send','Register','GetCode','Test');

        if(!in_array($action,  $whiteArr)){
            try {
                $jwtObject = Jwt::getInstance()->decode($token);
                //   $status =  $jwtObject->setSecretKey('easyswoole')->getStatus();
                $status =  $jwtObject->getStatus();
                switch ($status)
                {
                    case  1:
                        $this->adminInfo =  $jwtObject->getData();
                        //这边的数据不是即时的，后续要直接数据库查一遍
                        return true;
                        break;
                    case  -1:
                        $this->writeJson(Status::CODE_UNAUTHORIZED,  array(),'无效token');
                        return false;
                        break;
                    case  -2:
                        //如果最后访问时间
                        $this->adminInfo =  $jwtObject->getData();
                        return true;
                        break;
                }
            } catch (\EasySwoole\Jwt\Exception $e) {
                $this->writeJson(Status::CODE_UNAUTHORIZED,  array(),'token错误');
                return false;
            }
        }
        echo '不需要验证'.PHP_EOL;
        return true;
    }

    /**
     * 检测签名
     */
    private function checkSign($content,$timestamp, $nonce,$sign){
        if(md5($content.$timestamp.$nonce)==$sign){
            return true;
        }
        return false;
    }

    protected function writeJson($statusCode = 200, $result = null, $msg = null)
    {
        if (!$this->response()->isEndResponse()) {
            $content = HelpService::aesEncrypt(json_encode($result));
            $data = Array(
                "code" => $statusCode,
                "msg" => $msg,
                "result" => $content,
            );
            $this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT));
            $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
            if($statusCode==400||$statusCode==401){
                $this->response()->withStatus($statusCode);
            }else{
                $this->response()->withStatus(200);
            }

            return true;
        } else {
            return false;
        }
    }
}